package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwdeT;
import cn.e4j.modular.crm.mapper.GwdeTMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  选项分类设置 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-03-05
 */
@Service
public class GwdeTServiceImpl extends ServiceImpl<GwdeTMapper, GwdeT> {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdelist(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwdeDtos = this.baseMapper.listGwdePage(page, dto);
        return gwdeDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdeData(Dto dto) {
        List<Map<String, Object>> gwddDtos = this.baseMapper.getGwdelist(dto);
        return gwddDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwde017List(Dto dto) {
        List<Map<String, Object>> gwddDtos = this.baseMapper.getGwde017List(dto);
        return gwddDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Dto dto) {
        List<Map<String, Object>> gwdeDtos = this.baseMapper.getList(dto);
        return gwdeDtos;
    }

    @Transactional
    public Dto addData(List<GwdeT> gwdeTList) {
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        ShiroUser shiroUser = ShiroKit.getUser();
        QueryWrapper<GwdeT> gwdeTQueryWrapper_1 = new QueryWrapper<>();
        gwdeTQueryWrapper_1.eq("GWDEENT",shiroUser.getDBC());
        gwdeTQueryWrapper_1.eq("GWDE001",gwdeTList.get(0).getGWDE001());
        int query_count_1 = this.baseMapper.selectList(gwdeTQueryWrapper_1).size();
        QueryWrapper<GwdeT> gwdeTQueryWrapper_2 = new QueryWrapper<>();
        gwdeTQueryWrapper_2.eq("GWDEENT",shiroUser.getDBC());
        gwdeTQueryWrapper_2.eq("GWDE002",gwdeTList.get(0).getGWDE002());
        int query_count_2 = this.baseMapper.selectList(gwdeTQueryWrapper_2).size();
        if(query_count_1 > 0){
            outDto.setAppCode(403);
            return outDto;
        }
        if(query_count_2 > 0){
            outDto.setAppCode(404);
            return outDto;
        }
        for(GwdeT gwdeT:gwdeTList){
            QueryWrapper<GwdeT> gwdeTQueryWrapper1 = new QueryWrapper<>();
            gwdeTQueryWrapper1.eq("GWDEENT",shiroUser.getDBC());
            gwdeTQueryWrapper1.eq("GWDE001",gwdeT.getGWDE001());
            gwdeTQueryWrapper1.eq("GWDE003",gwdeT.getGWDE003());
            int query_count1 = this.baseMapper.selectList(gwdeTQueryWrapper1).size();
            QueryWrapper<GwdeT> gwdeTQueryWrapper2 = new QueryWrapper<>();
            gwdeTQueryWrapper2.eq("GWDEENT",shiroUser.getDBC());
            gwdeTQueryWrapper2.eq("GWDE001",gwdeT.getGWDE001());
            gwdeTQueryWrapper2.eq("GWDE004",gwdeT.getGWDE004());
            int query_count2 = this.baseMapper.selectList(gwdeTQueryWrapper2).size();
            if(query_count1 > 0){
                outDto.setAppCode(401);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            }
            if(query_count2 > 0){
                outDto.setAppCode(402);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            }
            gwdeT.setGWDEENT(shiroUser.getDBC());
            gwdeT.insert();
        }
        return outDto;
    }

    @Transactional
    public Dto editData(List<GwdeT> gwdeTList,String gwde001_old) {
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        ShiroUser shiroUser = ShiroKit.getUser();

        GwdeT gwdeT_del = new GwdeT();
        UpdateWrapper<GwdeT> gwdeTUpdateWrapper = new UpdateWrapper<>();
        gwdeTUpdateWrapper.eq("GWDEENT", shiroUser.getDBC());
        gwdeTUpdateWrapper.eq("GWDE001", gwde001_old);
        gwdeT_del.delete(gwdeTUpdateWrapper);

        QueryWrapper<GwdeT> gwdeTQueryWrapper_1 = new QueryWrapper<>();
        gwdeTQueryWrapper_1.eq("GWDEENT",shiroUser.getDBC());
        gwdeTQueryWrapper_1.eq("GWDE001",gwdeTList.get(0).getGWDE001());
        int query_count_1 = this.baseMapper.selectList(gwdeTQueryWrapper_1).size();
        QueryWrapper<GwdeT> gwdeTQueryWrapper_2 = new QueryWrapper<>();
        gwdeTQueryWrapper_2.eq("GWDEENT",shiroUser.getDBC());
        gwdeTQueryWrapper_2.eq("GWDE002",gwdeTList.get(0).getGWDE002());
        int query_count_2 = this.baseMapper.selectList(gwdeTQueryWrapper_2).size();
        if(query_count_1 > 0){
            outDto.setAppCode(403);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return outDto;
        }
        if(query_count_2 > 0){
            outDto.setAppCode(404);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return outDto;
        }
        for(GwdeT gwdeT:gwdeTList){
            QueryWrapper<GwdeT> gwdeTQueryWrapper1 = new QueryWrapper<>();
            gwdeTQueryWrapper1.eq("GWDEENT",shiroUser.getDBC());
            gwdeTQueryWrapper1.eq("GWDE001",gwdeT.getGWDE001());
            gwdeTQueryWrapper1.eq("GWDE003",gwdeT.getGWDE003());
            int query_count1 = this.baseMapper.selectList(gwdeTQueryWrapper1).size();
            QueryWrapper<GwdeT> gwdeTQueryWrapper2 = new QueryWrapper<>();
            gwdeTQueryWrapper2.eq("GWDEENT",shiroUser.getDBC());
            gwdeTQueryWrapper2.eq("GWDE001",gwdeT.getGWDE001());
            gwdeTQueryWrapper2.eq("GWDE004",gwdeT.getGWDE004());
            int query_count2 = this.baseMapper.selectList(gwdeTQueryWrapper2).size();
            if(query_count1 > 0){
                outDto.setAppCode(401);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            }
            if(query_count2 > 0){
                outDto.setAppCode(402);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            }
            gwdeT.setGWDEENT(shiroUser.getDBC());
            gwdeT.insert();
        }
        return outDto;
    }

    @Transactional
    public void delete(Dto dto) {
        GwdeT gwdeT = new GwdeT();
        UpdateWrapper<GwdeT> gwdeTUpdateWrapper = new UpdateWrapper<>();
        gwdeTUpdateWrapper.eq("GWDEENT", dto.getString("GWDEENT"));
        gwdeTUpdateWrapper.eq("GWDE001", dto.getString("GWDE001"));

        gwdeT.delete(gwdeTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwdeT> gwdeTList) {
        for (GwdeT gwdeT : gwdeTList) {
            UpdateWrapper<GwdeT> gwdeTUpdateWrapper = new UpdateWrapper<>();
            gwdeTUpdateWrapper.eq("GWDEENT", gwdeT.getGWDEENT());
            gwdeTUpdateWrapper.eq("GWDE001", gwdeT.getGWDE001());

            gwdeT.delete(gwdeTUpdateWrapper);
        }
    }
}
